package com.example.myproject.web;

import com.example.myproject.config.SpringUtil;
import com.example.myproject.exception.MyException;
import com.testautoconfig.TestAutoConfigService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.ui.ModelMap;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String index() {

        String url ="http://192.168.16.189:18090/qfcrm/api/apolloAuth/login";
        RestTemplate rt = new RestTemplate();
        MultiValueMap<String, String> requestEntityMap = new LinkedMultiValueMap<>();
        requestEntityMap.add("Username", "admin");
        requestEntityMap.add("password", "admin");
        requestEntityMap.add("security", "d31c1e25-62d7-4e45-a499-267b7e6c4c3d\n");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(requestEntityMap, headers);
//  执行HTTP请求
        String response = rt.postForObject(url, requestEntity, String.class);
        System.out.println(response);
        return  "1";
    }


    @RequestMapping("/json")
    public String json() throws MyException {
        String g = null;
        System.out.print("<<<<<<<<<<<<json");
        throw new MyException("xcx");
    }

    @RequestMapping("/testResponse")
    public String json(HttpServletRequest request,HttpServletResponse response) throws MyException {
      CookieUtil.addCookie(response,"test","223233343",-1);

      Cookie cookie = CookieUtil.getCookieByName(request,"test");
        System.out.println("value:"+(cookie!=null?cookie.getValue():""));
        return "1";

    }

    @RequestMapping("/")
    public String index(ModelMap map) {
        map.addAttribute("host", "http://blog.didispace.com");
        return "index";
    }


    @RequestMapping("/testEnableAuto")
    public String testEnableAuto(ModelMap map) {
        Object object = SpringUtil.getBean(TestAutoConfigService.class);
        System.out.println("0==="+(object== null));
        return "";
    }


}